<form class="cleanborder" id="filter_wrap" action="<?= base_url() ?>">

    <?

        function do_something($session, $type, $filter, $method) // my_data Array, acive/inactive String, $filter Array('hoteltype' => 'hotel'), popular/search String
        {

            switch($type):

                // Create http query string from session and add filter at the end
                case 'inactive':
                    $query=base_url('hotels/'.$method.'?'.http_build_query($session, '', '&amp;')).'&amp;'.key($filter).'%5B%5D='.$filter[key($filter)];

                    if(!isset($_GET['ajax_filter'])):
                        $query.= '&amp;ajax_filter=true';
                    endif;

                    return $query;
                    break;

                // Manualy create http query string and exclude filter element from it
                case 'active':

                    $link=base_url('hotels/'.$method.'?');
                    $do_prefix='';

                    // go trough my_data array
                    foreach($session as $key => $value):

                        // if element is array (filter) 
                        if(gettype($value) == 'array')
                        {

                            //loop troug each filter value
                            foreach($value as $key2 => $value2):


//                                echo $key.'<br>';
//                                echo key($filter).'<br>';
//                                echo $value2.'<br>';
//                                echo $filter[key($filter)].'<br>';
//                                echo '-----------<br>';
                                // add all element except submited (active) filter
                                if(($key == key($filter) && $value2 != $filter[key($filter)]) || $key != key($filter)):

                                    $link.=$do_prefix.$key.'%5B%5D='.$value2;
                                    $do_prefix='&';

                                endif;

                            endforeach;
                        }
                        // add evry element from session to query string
                        else
                        {

                            $link.=$do_prefix.$key.'='.$value;
                            $do_prefix='&amp;';
                        }



                    endforeach;

                    if(!isset($_GET['ajax_filter'])):
                        $link.= '&amp;ajax_filter=true';
                    endif;

                    return $link;

                    break;

            endswitch;

        }

        // find if elemet is posted in $_GET array
        function find_active($key, $value)
        {

            if(isset($_GET[$key]) && in_array($value, $_GET[$key]))
            {
                return 'active';
            }
            else
            {
                return 'inactive';
            }

        }
    ?>

    <fieldset>

        <div id="filterbox_options">

            <h3 class="filter_by"><?=lang('filter.filter_by')?></h3>

            <? if(isset($session['check_in'])): ?>

                    <!-- FILTER PRICE -->
                    <div id="filter_price" class="filterbox">

                        <div class="filtercategory">

                            <i class="icon_filter_arrow"></i>

                            <h3 class="filter_title"><?= lang('filter.price') ?> <span style="font-weight:normal;font-size:80%;text-transform:none;"></span></h3>

                        </div>

                        <div class="filteroptions">

                            <?
                            // TWO LINK TYPE 
                            // active and inactive
                            $method=$this->router->method;

                            $pr1_active=find_active('filter_price', 'pricerange_0_50');
                            $pr1_link=do_something($session, $pr1_active, array('filter_price' => 'pricerange_0_50'), $method);

                            $pr2_active=find_active('filter_price', 'pricerange_50_100');
                            $pr2_link=do_something($session, $pr2_active, array('filter_price' => 'pricerange_50_100'), $method);

                            $pr3_active=find_active('filter_price', 'pricerange_100_150');
                            $pr3_link=do_something($session, $pr3_active, array('filter_price' => 'pricerange_100_150'), $method);

                            $pr4_active=find_active('filter_price', 'pricerange_150_200');
                            $pr4_link=do_something($session, $pr4_active, array('filter_price' => 'pricerange_150_200'), $method);

                            $pr5_active=find_active('filter_price', 'pricerange_200_-1');
                            $pr5_link=do_something($session, $pr5_active, array('filter_price' => 'pricerange_200_-1'), $method);
                            ?>

                            <a rel="nofollow" href="<?= $pr1_link ?>" class="filterelement <?= $pr1_active ?>" data-holder="pr=0,50" data-id="pricerange_1">

                                <div class="filter_meta"><? if($pr1_active == 'active') echo $pr1_active; ?></div>
                                <div class="filter_item"><span>€&nbsp;0 - €&nbsp;49</span></div>

                            </a>

                            <a rel="nofollow" href="<?= $pr2_link ?>" class="filterelement <?= $pr2_active ?>" data-holder="pr=50,100" data-id="pricerange_2">

                                <div class="filter_meta"><? if($pr2_active == 'active') echo $pr2_active; ?></div>
                                <div class="filter_item"><span>€&nbsp;50 - €&nbsp;99</span></div>

                            </a>

                            <a rel="nofollow" href="<?= $pr3_link ?>" class="filterelement <?= $pr3_active ?>" data-holder="pr=100,150" data-id="pricerange_3">

                                <div class="filter_meta"><? if($pr3_active == 'active') echo $pr3_active; ?></div>
                                <div class="filter_item"><span>€&nbsp;100 - €&nbsp;149</span></div>

                            </a>

                            <a rel="nofollow" href="<?= $pr4_link ?>" class="filterelement <?= $pr4_active ?>" data-holder="pr=150,200" data-id="pricerange_4">

                                <div class="filter_meta"><? if($pr4_active == 'active') echo $pr4_active; ?></div>
                                <div class="filter_item"><span>€&nbsp;150 - €&nbsp;199</span></div>

                            </a>

                            <a rel="nofollow" href="<?= $pr5_link ?>" class="filterelement <?= $pr5_active ?>" data-holder="pr=200, -1" data-id="pricerange_5">

                                <div class="filter_meta"><? if($pr5_active == 'active') echo $pr5_active; ?></div>
                                <div class="filter_item"><span>€&nbsp;200 +</span></div>

                            </a>

                        </div>

                    </div>

                <? endif; ?>

            <!-- FILTER STAR RATING-->

            <?
                $found_star_rating=0;
                for($i=0; $i < 5; $i++):
                    if($filters_count['filter_class']['class-'.$i] > 0)
                        $found_star_rating=1;
                endfor;
            ?>

            <? if($found_star_rating): ?>
                    <div id="filter_class" class="filterbox">

                        <div class="filtercategory">

                            <i class="icon_filter_arrow"></i>

                            <h3 class="filter_title"><?=lang('filter.star_rating')?></h3>

                        </div>

                        <div class="filteroptions">

                            <?
                            // TWO LINK TYPE 
                            // active and inactive
                            $method=$this->router->method;

                            $class2_active=find_active('filter_class', 'class-2');
                            $class2_link=do_something($session, $class2_active, array('filter_class' => 'class-2'), $method);

                            $class3_active=find_active('filter_class', 'class-3');
                            $class3_link=do_something($session, $class3_active, array('filter_class' => 'class-3'), $method);

                            $class4_active=find_active('filter_class', 'class-4');
                            $class4_link=do_something($session, $class4_active, array('filter_class' => 'class-4'), $method);

                            $class5_active=find_active('filter_class', 'class-5');
                            $class5_link=do_something($session, $class5_active, array('filter_class' => 'class-5'), $method);

                            $class0_active=find_active('filter_class', 'class-0');
                            $class0_link=do_something($session, $class0_active, array('filter_class' => 'class-0'), $method);
                            ?>

                            <? if($filters_count['filter_class']['class-2'] > 0): ?>
                                <a rel="nofollow" href="<?= $class2_link ?>" class="filterelement <?= $class2_active ?>" data-id="class-2">

                                    <div class="filter_meta"><? if($class2_active == 'active') echo $class2_active; ?></div>
                                    <div class="filter_item"><span>2 <?= lang('filter.stars') ?></span> (<?= $filters_count['filter_class']['class-2'] ?>)</div>

                                </a> 
                            <? endif; ?>

                            <? if($filters_count['filter_class']['class-3'] > 0): ?>
                                <a rel="nofollow" href="<?= $class3_link ?>" class="filterelement <?= $class3_active ?>" data-id="class-3">

                                    <div class="filter_meta"><? if($class3_active == 'active') echo $class3_active; ?></div>
                                    <div class="filter_item"><span>3 <?= lang('filter.stars') ?></span> (<?= $filters_count['filter_class']['class-3'] ?>)</div>

                                </a>
                            <? endif; ?>

                            <? if($filters_count['filter_class']['class-4'] > 0): ?>
                                <a rel="nofollow" href="<?= $class4_link ?>" class="filterelement <?= $class4_active ?>" data-id="class-4">

                                    <div class="filter_meta"><? if($class4_active == 'active') echo $class4_active; ?></div>
                                    <div class="filter_item"><span>4 <?= lang('filter.stars') ?></span> (<?= $filters_count['filter_class']['class-4'] ?>)</div>

                                </a> 
                            <? endif; ?>

                            <? if($filters_count['filter_class']['class-5'] > 0): ?>
                                <a rel="nofollow" href="<?= $class5_link ?>" class="filterelement <?= $class5_active ?>" data-id="class-5">

                                    <div class="filter_meta"><? if($class5_active == 'active') echo $class5_active; ?></div>
                                    <div class="filter_item"><span>5 <?= lang('filter.stars') ?></span> (<?= $filters_count['filter_class']['class-5'] ?>)</div>

                                </a> 
                            <? endif; ?>

                            <? if($filters_count['filter_class']['class-0'] > 0): ?>

                                <a rel="nofollow" href="<?= $class0_link ?>" class="filterelement <?= $class0_active ?>" data-id="class-0">

                                    <div class="filter_meta"><? if($class0_active == 'active') echo $class0_active; ?></div>
                                    <div class="filter_item"><span>Unrated</span> (<?= $filters_count['filter_class']['class-0'] ?>)</div>

                                </a> 

                            <? endif; ?>

                        </div>

                    </div>
                <? endif; ?>

            <!-- FILTER HOTEL TYPE-->
            <div id="hoteltype" class="filterbox">

                <div class="filtercategory">

                    <i class="icon_filter_arrow"></i>

                    <h3 class="filter_title"><?= lang('filter.hotel_type') ?></h3>

                </div>

                <div class="filteroptions">

                    <?
                        // TWO LINK TYPE 
                        // active and inactive
                        $method=$this->router->method;

                        $hotel_active=find_active('hoteltype', 'hotel');
                        $hotel_link=do_something($session, $hotel_active, array('hoteltype' => 'hotel'), $method);

                        $apartment_active=find_active('hoteltype', 'apartment');
                        $apartment_link=do_something($session, $apartment_active, array('hoteltype' => 'apartment'), $method);

                        $villa_active=find_active('hoteltype', 'villa');
                        $villa_link=do_something($session, $villa_active, array('hoteltype' => 'villa'), $method);
                    ?>

                    <? if($filters_count['hotel_type']['hotel'] > 0): ?>
                            <a rel="nofollow" href="<?= $hotel_link ?>" class="filterelement <?= $hotel_active ?>" data-id="hotel">

                                <div class="filter_meta"><? if($hotel_active == 'active') echo $hotel_active; ?></div>
                                <div class="filter_item"><span><?= lang('filter.hotel') ?></span> (<?= $filters_count['hotel_type']['hotel'] ?>)</div>

                            </a> 
                        <? endif; ?>

                    <? if($filters_count['hotel_type']['apartment'] > 0): ?>
                            <a rel="nofollow" href="<?= $apartment_link ?>" class="filterelement <?= $apartment_active ?>" data-id="apartment">

                                <div class="filter_meta"><? if($apartment_active == 'active') echo $apartment_active; ?></div>
                                <div class="filter_item"><span><?= lang('filter.apartment') ?></span> (<?= $filters_count['hotel_type']['apartment'] ?>)</div>

                            </a>
                        <? endif; ?>

                    <? if($filters_count['hotel_type']['villa'] > 0): ?>
                            <a rel="nofollow" href="<?= $villa_link ?>" class="filterelement <?= $villa_active ?>" data-id="villa">

                                <div class="filter_meta"><? if($villa_active == 'active') echo $villa_active; ?></div>
                                <div class="filter_item"><span><?= lang('filter.villa') ?></span> (<?= $filters_count['hotel_type']['villa'] ?>)</div>

                            </a>
                        <? endif; ?>


                </div>

            </div>

            <!-- FILTER HOTEL TYPE-->
            <div id="filter_facilities" class="filterbox">

                <div class="filtercategory">

                    <i class="icon_filter_arrow"></i>

                    <h3 class="filter_title"><?= lang('filter.facility') ?></h3>

                </div>

                <div class="filteroptions">

                    <?
                        // TWO LINK TYPE 
                        // active and inactive
                        $method=$this->router->method;

                        $facilities=$filters_count['filter_facilities'];

                        //$this->firephp->log($facilities);

                        foreach($facilities as $facility => $key):



                            $facil_active=find_active('filter_facilities', $facility);
                            $facil_link=do_something($session, $facil_active, array('filter_facilities' => $facility), $method);
                            ?>

                            <a rel="nofollow" href="<?= $facil_link ?>" class="filterelement <?= $facil_active ?>" data-id="hotel">

                                <div class="filter_meta"><? if($facil_active == 'active') echo $facil_active; ?></div>
                                <div class="filter_item"><span><?= $key->title ?></span> (<?= $key->cnt ?>)</div>

                            </a>

                        <? endforeach; ?>







                </div>

            </div>

        </div>

    </fieldset>

</form>